<?php
namespace app\mapi\controller;
use app\mapi\model\MallHouseModel;

/**
 * 商品收藏控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class House extends Base{

    # 我的收藏
    public function houseList(){
        if(request()->isPost()){
            $Token = input('post.token','','trim');
            $Pages = input('post.page','','intval') ? input('post.page','','intval') : 0;
            $Limit = input('post.limit','','intval') ? input('post.limit','','intval') : 10;

            $FindUser = $this->checkLogin($Token);

            $MallHouse = new MallHouseModel;

            $ListHouse = $MallHouse->mList(['house_users'=>$FindUser['user_id']], 'house_goods,goods_name,goods_img', $Pages, $Limit);

            if(!empty($ListHouse)){
                foreach($ListHouse as $k => $v){
                    $ListHouse[$k]['house_count'] = $MallHouse->mCount(['house_goods'=>$v['house_goods']]);
                }

                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '暂无收藏';
            }

            $this->ShowAjax($Code, $Msgs, $ListHouse);
        }
    }

    # 加入收藏
    public function houseAdd(){
        if(request()->isPost()){
            $Goods = input('post.goods_id','','intval');
            $Token = input('post.token','','trim');

            if(empty($Goods)){
                $this->ShowAjax(2, '缺少goods_id参数');
            }

            $FindUser = $this->checkLogin($Token);

            $Param = [];
            $Param['house_goods'] = $Goods;
            $Param['house_users'] = $FindUser['user_id'];

            $MallHouse = new MallHouseModel;

            $BackHouse = $MallHouse->mAdd(['house_goods'=>$Goods, 'house_users'=>$FindUser['user_id']]);

            $this->ShowAjax($BackHouse['code'], $BackHouse['msgs']);
        }
    }

    # 删除收藏
    public function houseDel(){
        if(request()->isPost()){
            $Goods = input('post.goods_id','','intval');
            $Token = input('post.token','','trim');

            if(empty($Goods)){
                $this->ShowAjax(2, '缺少goods_id参数');
            }

            $FindUser = $this->checkLogin($Token);

            $Where = [];
            $Where['house_goods'] = $Goods;
            $Where['house_users'] = $FindUser['user_id'];

            $MallHouse = new MallHouseModel;

            $BackHouse = $MallHouse->mDel($Where);

            $this->ShowAjax($BackHouse['code'], $BackHouse['msgs']);
        }
    }

}